package cn.hubujyjs.trainingapp.config;

import cn.hubujyjs.trainingapp.interceptor.LoginInterceptor;
import cn.hubujyjs.trainingapp.interceptor.RefreshTokenInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * 注册拦截器
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LoginInterceptor loginInterceptor=new LoginInterceptor();
        RefreshTokenInterceptor refreshTokenInterceptor=new RefreshTokenInterceptor(stringRedisTemplate);
        registry.addInterceptor(loginInterceptor)
                .excludePathPatterns("/api/user/code", "/api/user/login")
                .order(1);
        registry.addInterceptor(refreshTokenInterceptor)
                .addPathPatterns("/**")  //默认就是拦截所有请求
                .order(0);  //值越小先执行
    }
}
